const Controller = require('../core/base_controller')

class HomeController extends Controller {
  async index() {
    await this.ctx.render('index');
  }

  async login() {
    const { ctx, app } = this

    const client = ctx.grpc.com.jstide.grpc.user.userService
    const { exception, user } = await client.login(ctx.request.body)

    if (exception) {
      this.exception(exception)
      return
    }

    // await app.sessionStore.set('user', user)
    ctx.login(user)

    this.success(user)
  }

  async loguser() {
    // const user = await this.app.sessionStore.get('user')
    const { user } = this.ctx
    if (user) {
      this.success(user)
    } else {
      this.error(401)
    }
  }

  async logout() {
    // await this.app.sessionStore.destroy('user')
    this.ctx.logout()
    this.success()
  }

  // async proxy() {
  //   const ctx = this.ctx;
  //   // use roadhog mock api first
  //   const url = this.app.config.assets.url + ctx.path + '?' + ctx.querystring;

  //   const res = await this.ctx.curl(url, {
  //     method: this.ctx.method,
  //   });
  //   ctx.body = res.data;
  //   ctx.status = res.status;
  // }
}

module.exports = HomeController;
